﻿namespace MefContrib.Models.Provider.Tests
{
    using System;
    using NUnit.Framework;

    [TestFixture]
    public class ComposableTypeTests
    {
        [Test]
        public void DeclaringTypeShouldReturnTypeOfClass()
        {
            ComposableType composable =
                new ComposableType(typeof(FakePart));

            Assert.IsTrue(composable.DeclaringType == typeof(FakePart));
        }

        [Test]
        public void ReturnTypeShouldReturnTypeOfClass()
        {
            ComposableType composable =
                new ComposableType(typeof(FakePart));

            Assert.IsTrue(composable.ReturnType == typeof(FakePart));
        }

        [Test]
        public void TypeShouldBeReadable()
        {
            ComposableType composable =
                new ComposableType(typeof(FakePart));

            Assert.IsTrue(composable.IsReadable);
        }

        [Test]
        public void TypeShouldNotBeWritable()
        {
            ComposableType composable =
                new ComposableType(typeof(FakePart));

            Assert.IsFalse(composable.IsWritable);
        }

        [Test, ExpectedException(typeof(InvalidOperationException))]
        public void SetValueShouldThrowException()
        {
            ComposableType composable =
                new ComposableType(typeof(FakePart));

            FakePart part =
                new FakePart();

            composable.SetValue(part, null);
        }

        [Test]
        public void GetValueShouldReturnPassedInInstance()
        {
            ComposableType composable =
                new ComposableType(typeof(FakePart));

            FakePart part =
                new FakePart();

            object returnedValue =
                composable.GetValue(part);

            Assert.AreSame(part, returnedValue);
        }

        [Test]
        public void ShouldAlwaysRequireInstance()
        {
            ComposableType composable =
                new ComposableType(typeof(FakePart));

            Assert.IsTrue(composable.IsInstanceNeeded);
        }
    }

    public class FakePart
    {
    }
}